package com.ruoyi.web.controller.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;

import java.io.IOException;
import java.io.InputStream;

public class OSSUtil {
    static private final String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    //从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    static private final String accessKeyId = "LTAI5tLFS2wnahdu56aujrE5";
    static private final String accessKeySecret = "PviNt7uRXnOzCkNdAvPhpXT2T8lAib";
    // 填写Bucket名称，例如examplebucket。
    static private final String bucketName = "wzn-boot";

    static private final String furl = "https://wzn-boot.oss-cn-beijing.aliyuncs.com/";



    public static String uploadFile(String filePath,InputStream inputStream) throws IOException {
        String objectName = "images/";
        objectName = objectName + filePath;

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        PutObjectResult result = ossClient.putObject(bucketName, objectName, inputStream);

        ossClient.shutdown();
        return furl + objectName;
    }
}
